package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    /**
     * 新增套餐
     *
     * @param dto
     */
    @Override
    public void addCategory(CategoryDTO dto) {
        Category category = new Category();
        //补充数据
        BeanUtils.copyProperties(dto,category);
        category.setStatus(StatusConstant.ENABLE);
        /*category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        Long id = BaseContext.getCurrentId();
        category.setCreateUser(id);
        category.setUpdateUser(id);
        BaseContext.removeCurrentId();*/
        //调用mapper层
        categoryMapper.addCategory(category);
    }

    /**
     * 分类分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult pagesGetCate(CategoryPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<Category> page = categoryMapper.pagesGetCate(dto);
        return new  PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id删除菜品分类
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //判断分类下是否有菜品
        Integer count = dishMapper.countByCate(id);
        Integer countSet = setMealMapper.countByCate(id);
        if(count>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        if(countSet>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.deleteById(id);
    }

    /**
     * 修改菜品分类
     *
     * @param dto
     */
    @Override
    public void modifyCate(CategoryDTO dto) {
        //为category补充数据
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        /*category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());*/
        categoryMapper.modifyCate(category);
    }

    /**
     * 启动/禁用菜品分类
     *
     * @param status
     * @param id
     */
    @Override
    public void cateStatus(Integer status, Long id) {
        Category category = Category.builder()
                .id(id)
                .status(status)
                /*.updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())*/
                .build();
        categoryMapper.modifyCate(category);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> getByType(Integer type) {
        return categoryMapper.getByType(type);
    }
}
